package com.example.emailWeb.Socket.test.sendObject;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class ClientB {
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        DataBuff dataBuff = new DataBuff();
        Scanner scanner = new Scanner(System.in);
        String Str = "";
        ObjectInputStream ois = null;

        while (!"n".equalsIgnoreCase(Str)){
            Str = scanner.nextLine();

            Socket socket = new Socket("127.0.0.1", Port.ServerPort);
            dataBuff.info = Str;
            send(socket,dataBuff);
            ois = new ObjectInputStream(socket.getInputStream());
            DataBuff dataBuff1 = (DataBuff) ois.readObject();
            if(ois!=null){
                ois.close();
            }
            socket.close();
            System.out.println(dataBuff1);
        }
    }
    public static void send(Socket socket, DataBuff dataBuff) throws IOException {
        ObjectOutputStream os = null;
        os = new ObjectOutputStream(socket.getOutputStream());
        try {
            os.writeObject(dataBuff);
            socket.getOutputStream().flush();
        } catch (
                IOException e) {
            e.printStackTrace();
        }finally {
//            socket.shutdownOutput();
            /*if(os!=null){
                os.close();
            }*/
        }
    }
}
